package controller.welcome;

import controller.common.BaseController;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import properties.SystemProperty;
import util.JsonUtil;

import java.util.Map;

@Controller
@RequestMapping("welcome/singleLogin")
public class SingleLoginController extends BaseController {

    @RequestMapping(value = {"jianYan","gongXu"})
    public String singleLogin(String url,Map modalMap) {

        GetMethod getMethod = null;
        String responseMsg = null;
        try {
            getMethod = new GetMethod(SystemProperty.getSelectExpertAccountUrl() + "?idCard=" + getLoginAccount().getExpertBase().getIdCard());
            new HttpClient().executeMethod(getMethod); // 执行postMethod,调用http接口
            responseMsg = getMethod.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            if (getMethod != null) {
                getMethod.releaseConnection();
            }
            modalMap.put("errorMessage", e.getMessage());
            return "welcome/error/errorPage";
        }
        Map responseMap = (Map) JsonUtil.json2Obj(responseMsg, Map.class);
        if ("zj001".equals(responseMap.get("code"))) {
            Map temp = (Map) responseMap.get("data");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("redirect:");
            stringBuilder.append(url);
            stringBuilder.append("?account=");
            stringBuilder.append(temp.get("account"));
            stringBuilder.append("&password=");
            stringBuilder.append(temp.get("password"));
            return stringBuilder.toString();
        } else {
            modalMap.put("errorMessage", responseMsg);
            return "welcome/error/errorPage";
        }

    }

}
